package com.summer.user.center.controller;


import cn.hutool.core.util.StrUtil;
import com.summer.common.model.AjaxResult;
import com.summer.user.center.model.Department;
import com.summer.user.center.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author likun
 * 2018/5/22 11:13
 **/
@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ResponseBody
    @RequestMapping("/findAllDepartmentTree")
    public Department findAllDepartmentTree() {
        return departmentService.findAllDepartmentTree();
    }

    @ResponseBody
    @RequestMapping("/addDepartment")
    public AjaxResult addDepartment(String name, Long parentId) {
        if (StrUtil.isNotEmpty(name) && parentId != null) {
            return new AjaxResult(departmentService.addDepartment(name, parentId));
        } else {
            return new AjaxResult(false, "参数不能为空");
        }
    }

    @ResponseBody
    @RequestMapping("/editDepartment")
    public AjaxResult editDepartment(Long id, String name) {
        if (id != null && StrUtil.isNotEmpty(name)) {
            return new AjaxResult(departmentService.editDepartment(id, name));
        }
        return new AjaxResult(false);
    }

    @ResponseBody
    @RequestMapping("/deleteDepartment")
    public AjaxResult deleteDepartment(Long id) {
        if (id != null) {
            return new AjaxResult(departmentService.deleteDepartment(id));
        }
        return new AjaxResult(false);
    }
}
